<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="css/mui.min.css"/>
	</head>

	<body>
		<style>
			body {
				background: #fff;

		</style>
		<script src="js/mui.min.js"></script>
		<script src="js/libs/zepto_1.1.3.js"></script>
		<script src="js/common/common.js"></script>
		<script type="text/javascript">
			mui.init()
			
			localStorage.removeItem("books")
			localStorage.removeItem("text")
			localStorage.removeItem("state")
			mui.plusReady(function() {
				var data = JSON.parse(localStorage.getItem("data"));
				if(data == null) {
					mui.openWindow({
						url: 'html/loginregistration/login.html',
						id: 'login.html',
						waiting: {
							autoShow: false
						},
						show: {
							aniShow: "none"
						}
					})
				} else {
					mui.openWindow({
						url: 'html/home/index.html',
						id: 'index.html',
						waiting: {
							autoShow: false
						},
						show: {
							aniShow: "none"
						}
					})
				}
			})

			common.ajax('app/version/2', "", function(data) {
				console.log(data)
			})

			var wgtVer = null;

			function plusReady() {
				// ......
				// 获取本地应用资源版本号

				common.ajax('app/version/1', "", function(data) {
					if(data.code == 200) {
						if(data.data.forceUpdate == 0) {
							plus.runtime.getProperty(plus.runtime.appid, function(inf) {
								wgtVer = inf.version;
								if(data.data.versionNum != wgtVer) {
									/*downWgt(data.data.url)*/
									var btnArray = ['是', '否'];
									mui.confirm('' + data.data.versionNum + '版本更新,是否更新', '发现新版本', btnArray, function(e) {
										if(e.index == 0) {
											downWgt(data.data.url)
										}
									})
									//								checkUpdate()
								}
								//							console.log("当前应用版本：" + wgtVer);
								//					checkUpdate()

							});
						} else {
							downWgt(data.data.url)
						}
					}

				})
			}
			if(!(navigator.userAgent.toUpperCase().indexOf('IPHONE') != -1)) {
				if(window.plus) {
					plusReady();
				} else {
					document.addEventListener('plusready', plusReady, false);
				}
			}
			// 检测更新
			/*var checkUrl = "http://192.168.0.116:8080/api/app/version/1";

			function checkUpdate() {
				plus.nativeUI.showWaiting("检测更新...");
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function() {
					switch(xhr.readyState) {
						case 4:
							plus.nativeUI.closeWaiting();
							if(xhr.status == 200) {
								console.log("检测更新成功：" + xhr.responseText);
								var newVer = xhr.responseText;
								if(wgtVer && newVer && (wgtVer != newVer)) {
									downWgt(); // 下载升级包
								} else {
									plus.nativeUI.alert("无新版本可更新！");
								}
							} else {
								console.log("检测更新失败！");
								plus.nativeUI.alert("检测更新失败！");
							}
							break;
						default:
							break;
					}
				}
				xhr.open('GET', checkUrl);
				xhr.send();
			}*/

			// 下载wgt文件
			//			var wgtUrl = "http://demo.dcloud.net.cn/test/update/H5EF3C469.wgt";

			function downWgt(wgtUrl) {
				plus.nativeUI.showWaiting("下载中...");
				var task = plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, function(d, status) {
					if(status == 200) {
						console.log("下载wgt成功：" + d.filename);
						installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			}

			function installWgt(path) {
				plus.nativeUI.showWaiting("安装wgt文件...");
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件成功！");
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			}
		</script>
	</body>

</html>